package com.ruoyi.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.DictConstants;
import lombok.RequiredArgsConstructor;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.tws.domain.TwsCompany;
import com.ruoyi.tws.service.ITwsCompanyService;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 货源方Controller
 * 
 * @author wms
 * @date 2022-01-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/company")
public class TwsCompanyController extends BaseController {
    private final ITwsCompanyService iTwsCompanyService;

    /**
     * 查询货源方列表
     */
    @PreAuthorize(hasPermi="tws:company:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, TwsCompany twsCompany) {
        LambdaQueryWrapper<TwsCompany> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(twsCompany.getCompanyName())) {
            lqw.like(TwsCompany::getCompanyName, twsCompany.getCompanyName());
        }
        if (StringUtils.isNotBlank(twsCompany.getShortCode())) {
            lqw.eq(TwsCompany::getShortCode, twsCompany.getShortCode());
        }
        if (StringUtils.isNotBlank(twsCompany.getTaxNo())) {
            lqw.eq(TwsCompany::getTaxNo, twsCompany.getTaxNo());
        }
        if (StringUtils.isNotBlank(twsCompany.getTel())) {
            lqw.eq(TwsCompany::getTel, twsCompany.getTel());
        }
        if (StringUtils.isNotBlank(twsCompany.getLinker())) {
            lqw.eq(TwsCompany::getLinker, twsCompany.getLinker());
        }
        if (StringUtils.isNotBlank(twsCompany.getLinktel())) {
            lqw.eq(TwsCompany::getLinktel, twsCompany.getLinktel());
        }
        if (twsCompany.getIsCert() != null) {
            lqw.eq(TwsCompany::getIsCert, twsCompany.getIsCert());
        }
        if (twsCompany.getState() != null) {
            lqw.eq(TwsCompany::getState, twsCompany.getState());
        }
        lqw.orderByDesc(TwsCompany::getCreateTime);
        IPage<TwsCompany> list = iTwsCompanyService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 所有货源方
     */
    @PreAuthorize(hasPermi="tws:company:list")
    @GetMapping("/listAll")
    public AjaxResult listAll(TwsCompany twsCompany) {
        LambdaQueryWrapper<TwsCompany> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(twsCompany.getCompanyName())) {
            lqw.like(TwsCompany::getCompanyName, twsCompany.getCompanyName());
        }
        if (StringUtils.isNotBlank(twsCompany.getShortCode())){
            lqw.eq(TwsCompany::getShortCode, twsCompany.getShortCode());
        }
        if (StringUtils.isNotBlank(twsCompany.getTaxNo())) {
            lqw.eq(TwsCompany::getTaxNo, twsCompany.getTaxNo());
        }
        if (StringUtils.isNotBlank(twsCompany.getTel())){
            lqw.eq(TwsCompany::getTel, twsCompany.getTel());
        }
        if (StringUtils.isNotBlank(twsCompany.getLinker())){
            lqw.eq(TwsCompany::getLinker, twsCompany.getLinker());
        }
        if (StringUtils.isNotBlank(twsCompany.getLinktel())){
            lqw.eq(TwsCompany::getLinktel, twsCompany.getLinktel());
        }
        if (twsCompany.getIsCert() != null) {
            lqw.eq(TwsCompany::getIsCert, twsCompany.getIsCert());
        }
        if (twsCompany.getState() != null) {
            lqw.eq(TwsCompany::getState, twsCompany.getState());
        }
        List<TwsCompany> list = iTwsCompanyService.list(lqw);
        return AjaxResult.success(list);
    }

    /**
     * 审核中计数
     */
    @GetMapping("/getCertNum")
    public AjaxResult getCertNum() {
        LambdaQueryWrapper<TwsCompany> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TwsCompany::getIsCert, DictConstants.IS_CERT.SHZ.getKey());
        int count = iTwsCompanyService.count(lqw);
        return AjaxResult.success(count);
    }

    /**
     * 导出货源方列表
     */
    @PreAuthorize(hasPermi = "tws:company:export")
    @Log(title = "货源方", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TwsCompany twsCompany) throws IOException
    {
        LambdaQueryWrapper<TwsCompany> lqw = new LambdaQueryWrapper<TwsCompany>(twsCompany);
        List<TwsCompany> list = iTwsCompanyService.list(lqw);
        ExcelUtil<TwsCompany> util = new ExcelUtil<TwsCompany>(TwsCompany. class);
        util.exportExcel(response, list, "company");
    }

    /**
     * 获取货源方详细信息
     */
    @PreAuthorize(hasPermi="tws:company:query")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iTwsCompanyService.getById(id));
    }

    /**
     * 获取货源方
     */
    @PreAuthorize(hasPermi="tws:company:query")
    @GetMapping(value = "/getCompanyByUserId/{userId}")
    public AjaxResult getCompanyByUserId(@PathVariable("userId") Long userId) {
        TwsCompany twsCompany = iTwsCompanyService.getOne(new LambdaQueryWrapper<TwsCompany>()
                                            .eq(TwsCompany::getUserId, userId));
        return AjaxResult.success(twsCompany);
    }

    /**
     * 新增货源方
     */
    @PreAuthorize(hasPermi="tws:company:add")
    @Log(title = "货源方", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TwsCompany twsCompany) {
        return toAjax(iTwsCompanyService.add(twsCompany));
    }

    /**
     * 修改货源方
     */
    @PreAuthorize(hasPermi="tws:company:edit")
    @Log(title = "货源方", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TwsCompany twsCompany) {
        return toAjax(iTwsCompanyService.edit(twsCompany));
    }

    /**
     * 货源方审核
     */
    @PreAuthorize(hasPermi="tws:company:edit")
    @Log(title = "货源方", businessType = BusinessType.UPDATE)
    @PostMapping("/cert")
    public AjaxResult cert(@RequestBody TwsCompany twsCompany) {
        return toAjax(iTwsCompanyService.cert(twsCompany));
    }

    /**
     * 货源方禁/启用
     */
    @PreAuthorize(hasPermi="tws:company:edit")
    @Log(title = "货源方", businessType = BusinessType.UPDATE)
    @PostMapping("/stateChange")
    public AjaxResult stateChange(@RequestBody TwsCompany twsCompany) {
        return toAjax(iTwsCompanyService.stateChange(twsCompany));
    }

    /**
     * 删除货源方
     */
    @PreAuthorize(hasPermi="tws:company:remove")
    @Log(title = "货源方", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iTwsCompanyService.deletes(Arrays.asList(ids)));
    }

}
